package com.zhanghu.backendZray.controller.album;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhanghu.backendZray.annotation.MenuNeed;
import com.zhanghu.backendZray.cache.ContextCache;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import com.zhanghu.backendZray.pojo.request.OnlyIdRequest;
import com.zhanghu.backendZray.pojo.request.album.AlbumCollectionPageRequest;
import com.zhanghu.backendZray.pojo.request.album.AlbumCollectionRequest;
import com.zhanghu.backendZray.pojo.request.album.InviteFamilyGroupRequest;
import com.zhanghu.backendZray.pojo.request.album.InviteFamilyResultRequest;
import com.zhanghu.backendZray.pojo.request.user.UserPageRequest;
import com.zhanghu.backendZray.pojo.vo.UserVo;
import com.zhanghu.backendZray.pojo.vo.albumCollection.AlbumCollectionVo;
import com.zhanghu.backendZray.pojo.vo.albumCollection.IntervalPendingTaskVo;
import com.zhanghu.backendZray.service.AlbumCollectionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 相册合集表 前端控制器
 * </p>
 *
 * @author zhangdong
 * @since 2024-08-17
 */
@RestController
@RequestMapping("/albumCollection")
@RequiredArgsConstructor
public class AlbumCollectionController {
    private final AlbumCollectionService albumCollectionService;

    /**
     * 只新增名称
     */
    @MenuNeed(menuName = "album_collection_manage")
    @PostMapping("add")
    public ApiResponse add(@RequestBody @Validated(AlbumCollectionRequest.Create.class) AlbumCollectionRequest albumCollectionRequest) {
        albumCollectionService.add(albumCollectionRequest);
        return ApiResponse.ok();
    }

    /**
     * 只修改名称
     */
    @MenuNeed(menuName = "album_collection_manage")
    @PostMapping("update")
    public ApiResponse update(@RequestBody @Validated(AlbumCollectionRequest.Update.class) AlbumCollectionRequest albumCollectionRequest) {
        albumCollectionService.update(albumCollectionRequest);
        return ApiResponse.ok();
    }

    /**
     * 配置封面
     */
    @MenuNeed(menuName = "album_collection_manage")
    @PostMapping("coverFileSetting")
    public ApiResponse coverFileSetting(@RequestBody AlbumCollectionRequest albumCollectionRequest) {
        albumCollectionService.coverFileSetting(albumCollectionRequest);
        return ApiResponse.ok();
    }

    /**
     * 配置合集照片
     */
    @MenuNeed(menuName = "album_collection_manage")
    @PostMapping("fileSetting")
    public ApiResponse fileSetting(@RequestBody AlbumCollectionRequest albumCollectionRequest) {
        albumCollectionService.fileSetting(albumCollectionRequest);
        return ApiResponse.ok();
    }

    @MenuNeed(menuName = "album_collection_manage")
    @PostMapping("page")
    public ApiResponse<Page<AlbumCollectionVo>> page(@RequestBody AlbumCollectionPageRequest request) {
        Page<AlbumCollectionVo> page = albumCollectionService.page(request);
        return ApiResponse.ok(page);
    }

    @MenuNeed(menuName = "album_collection_manage")
    @PostMapping("delete")
    public ApiResponse add(@RequestBody OnlyIdRequest onlyIdRequest) {
        albumCollectionService.delete(onlyIdRequest.getId().intValue());
        return ApiResponse.ok();
    }

    /**
     * 相册合集展示-查询所有
     */
    @MenuNeed(menuName = "album_collection_show")
    @PostMapping("listAll")
    public ApiResponse<List<AlbumCollectionVo>> listAll() {
        List<AlbumCollectionVo> list = albumCollectionService.listAll();
        return ApiResponse.ok(list);
    }

    /**
     * 获取合集中的封面文件id
     */
    @PostMapping("getPreviewFileId")
    public ApiResponse<Integer> getPreviewFileId(@RequestBody OnlyIdRequest onlyIdRequest) {
        Integer previewFileId = albumCollectionService.getPreviewFileId(onlyIdRequest.getId().intValue());
        return ApiResponse.ok(previewFileId);
    }

    /**
     * 获取合集中的所有文件id集合
     */
    @PostMapping("listAllFileId")
    public ApiResponse<List<Integer>> listAllFileId(@RequestBody OnlyIdRequest onlyIdRequest) {
        List<Integer> fileIdList = albumCollectionService.listAllFileId(onlyIdRequest.getId().intValue());
        return ApiResponse.ok(fileIdList);
    }

    /**
     * 邀请家庭成员
     */
    @MenuNeed(menuName = "family_group_setting")
    @PostMapping("inviteFamilyGroup")
    public ApiResponse inviteFamilyGroup(@Validated @RequestBody InviteFamilyGroupRequest inviteFamilyGroupRequest) {
        albumCollectionService.inviteFamilyGroup(inviteFamilyGroupRequest);
        return ApiResponse.ok();
    }

    /**
     * 用户提交结果
     */
    @PostMapping("submitInviteResult")
    public ApiResponse submitInviteResult(@Validated @RequestBody InviteFamilyResultRequest inviteFamilyResultRequest) {
        albumCollectionService.submitInviteResult(inviteFamilyResultRequest);
        return ApiResponse.ok();
    }

    /**
     * 分页查询所有家庭成员
     */
    @MenuNeed(menuName = "family_group_setting")
    @PostMapping("familyGroupPage")
    public ApiResponse<Page<UserVo>> familyGroupPage(@RequestBody UserPageRequest userPageRequest) {
        Page<UserVo> page = albumCollectionService.familyGroupPage(userPageRequest);
        return ApiResponse.ok(page);
    }

    /**
     * 定期获取待办事项相关
     */
    @GetMapping("intervalPendingTask")
    public ApiResponse<IntervalPendingTaskVo> listAllPendingTask() {
        return ApiResponse.ok(albumCollectionService.pendingTaskMesPush(ContextCache.currentUserId()));
    }

}
